<?php
class Pagination {
/*
Người tạo	: Lại Ngọc Thái Châu - Team 0
Module 	: Pagination
Chức năng	: phan trang
 */
	public $url;
	public $total_page;
	public $cur_page;
	
	// ham khoi tao phan trang
  	public function init($_url,$_total_book,$_limit,$_cur_page){
  	     $_total_page = floor($_total_book/$_limit);
            if ($_total_book % $_limit != 0) $_total_page++;

            $this->url = $_url;
            $this->total_page = $_total_page;
            $this->cur_page = $_cur_page;

  	}
    
   public function renderFront(){
        $html = "";
        if ($this->total_page > 1){
            $html .= '<div class="pagination"><ul>';
            if($this->total_page < 11){
                for($i = 1;$i <= $this->total_page;$i++){
                    if($i == $this->cur_page)
                        $html .= '<li class="active"><a title="'.$i.'">'.$i.'</a></li>';
                    else
                        $html .= '<li class=""><a title="'.$i.'" href="'.$this->url.$i.'" rel="follow" >'.$i.'</a></li>';
                }           
            }else if ($this->cur_page == 1){
                for($i = 1;$i < 11;$i++){
                    if($i == 1)
                        $html .= '<li class="active"><a title="'.$i.'">'.$i.'</a></li>';
                    else
                        $html .= '<li class=""><a title="'.$i.'" href="'.$this->url.$i.'">'.$i.'</a></li>';
                }
                if($this->total_page > 10){
                    $html .= '<li class="ui-listItem"><a title="Tiếp theo" href="'.$this->url.'2" >&gt;</a></li>';                 
                }                           
            }else if($this->cur_page == $this->total_page){
                $html .= '<li class=""><a title="Trở lại" href="'. $this->url.($this->total_page- 1)  .'">&lt;</a></li>';
                for($i = $this->total_page-9;$i <= $this->total_page;$i++){
                    if($i == $this->total_page)
                        $html .= '<li class="active"><a title="'.$i.'">'.$i.'</a></li>';
                    else
                        $html .= '<li class=""><a title="'.$i.'" href="'. $this->url.$i  .'">'.$i.'</a></li>';
                }                   
            }else{
                
                $j = $this->total_page - ($this->cur_page + 9);
                if($j < 0){
                    $html .= '<li class=""><a title="Trở lại" href="'. $this->url.($this->cur_page-1)  .'">&lt;</a></li>';
                    for($i = $this->cur_page + $j;$i <= $this->total_page;$i++){
                        if($i == $this->cur_page)
                            $html .= '<li class="active"><a title="'.$i.'">'.$i.'</a></li>';
                        else
                            $html .= '<li class=""><a title="'.$i.'" href="'. $this->url.$i  .'">'.$i.'</a></li>';
                    }                                       
                }else{
                    $html .= '<li class=""><a title="Trở lại" href="'. $this->url.($this->cur_page-1)  .'">&lt;</a></li>';
                    for($i = $this->cur_page;$i < $this->cur_page + 10;$i++){
                        if($i == $this->cur_page)
                            $html .= '<li class="active"><a title="'.$i.'" class="selected">'.$i.'</a></li>';
                        else
                            $html .= '<li class=""><a title="'.$i.'" href="'. $this->url.$i  .'">'.$i.'</a></li>';
                    }
                    if($this->total_page > $this->cur_page + 9 ){
                        $html .= '<li class=""><a title="Tiếp theo" href="'. $this->url.($this->cur_page + 1)  .'" >&gt;</a></li>';                   
                    }                                       
                }                                
            }
            $html .= '</ul></div>';
        }

        return $html;
    }
}	